/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPack;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Graphics;
import MaterialPack.*;
import Supporter.*;
import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.rms.RecordStoreException;
import javax.obex.HeaderSet;

/**
 *
 * @author Van persie
 */
public class MenuCanvas extends Canvas implements Runnable, CommandListener {

    private MainMIDlet main = null;
    private Image background = null;
    private Vector buttons = null;
    private Image Title = null;
    private int height, width;
    private Player myplayer;
    public MenuCanvas(MainMIDlet midlet,Player ss) throws IOException, MediaException {
        main = midlet;
        height = this.getHeight();
        width = this.getWidth();
        myplayer=ss;
        
        try {
            LoadResource();
        } catch (Exception e) {
        }

        //this.sizeChanged(UP, UP);  
    }

    public void LoadResource() throws IOException, MediaException {
        if (buttons != null) {
            buttons.removeAllElements();
        }
        buttons = new Vector();
        ImageSupporter imgSupporter = new ImageSupporter();
        //background = ImageSupporter.scaleImage(  Image.createImage("/Background.png"),width,height);
        Image temp = imgSupporter.getImage("/save-background.jpg");
        Title = imgSupporter.getImage("/menu-title1.png");
        Title = ImageSupporter.scaleImage(Title, 165, 85);
        background = ImageSupporter.scaleImage(temp, width, height);

        buttons.addElement(new Button("/button-start-over.png", null, 200, 4 * height / 8, width / 4, height / 8));
        buttons.addElement(new Button("/button-continue-over.png", null, 170, 6 * height / 9, width / 2, height / 8));
        buttons.addElement(new Button("/button-quit-over.png", null,50,7 * height / 8, width / 4, height / 8));

        buttons.addElement(new CheckedButton("/button-speaker-over.png", "/button-speaker.png","/button-speaker.png", width-30, 0, 50, 50));
        myplayer.start();
    }

    protected void sizeChanged(int w, int h) {
        width = w;
        height = h;
        try {
            LoadResource();
        } catch (Exception e) {
        }
    }

    protected void pointerPressed(int x, int y) {
        for (int i = 0; i < buttons.size(); i++) {
            ((Button) buttons.elementAt(i)).Press(x, y);
        }
        repaint();
    }

    protected void pointerReleased(int x, int y) {
        for (int i = 0; i < buttons.size(); i++) {
            if (((Button) buttons.elementAt(i)).Release(x, y)) {
                if (((Button) buttons.elementAt(i)).isContained(x, y) == true) {
                    switch (i) {
                        case 0:
                            try {
                                try {
                                    myplayer.stop();
                                } catch (MediaException ex) {
                                    ex.printStackTrace();
                                }
                        try {
                            main.SwitchTo(MainMIDlet.SCENE_SETNAME);
                        } catch (MediaException ex) {
                            ex.printStackTrace();
                        }
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                            break;
                        case 1:
                            try {
                                SaveLoadSupporter.OpenRecordStore("AmazingPet");
                                if (SaveLoadSupporter.GetNumRecord()>0)
                                {
                                main.toLoadScene();
                            try {
                                myplayer.stop();
                            } catch (MediaException ex) {
                                ex.printStackTrace();
                            }
                                }
                            } catch (RecordStoreException ex) {
                                ex.printStackTrace();
                            }
                            break;
                        case 2:
                            main.exitMIDlet();
                            break;
                        case 3:
                            GlobalVariables.sound=!GlobalVariables.sound;
                            break;
                    }
                }
            }
        }
        repaint();
    }

    protected void keyPressed(int keyCode) {

        switch (this.getKeyCode(keyCode)) {
            case UP:
                main.destroyApp(true);
                break;
        }
    }

    public void commandAction(Command command, Displayable displayable) {
    }

    public void paint(Graphics g) {
        g.drawImage(background, 0, 0, Graphics.TOP | Graphics.LEFT);
        g.drawImage(Title, 0, 0,  Graphics.TOP | Graphics.LEFT);
        for (int i = 0; i < buttons.size(); i++) {
            ((Button) buttons.elementAt(i)).paint(g);
        }

    }

    public void run() {
    }
}
